package me.boger.venusplayer.inject

import android.content.Context
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.WindowManager

/**
 * Created by hb.chen on 2017/10/13.
 */

var displayMetrics: DisplayMetrics? = null

private fun initDisplayMetricsIfNeed(ctx: Context) {
    if (displayMetrics != null) {
        return
    }
    displayMetrics = DisplayMetrics()
    val wm = ctx.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    wm.defaultDisplay.getMetrics(displayMetrics)
}

fun Context.toPixelFromDIP(value: Float): Float {
    initDisplayMetricsIfNeed(this)
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, displayMetrics)
}

fun Context.toDIPFromPixel(value: Float): Float {
    initDisplayMetricsIfNeed(this)
    return value / displayMetrics!!.density

}


